﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using System.Windows.Forms.DataVisualization.Charting;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Portal.Extensions;
using System.Drawing;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class SLAController : BaseController
    {
        // 声明变量
        private ISLA sla;
        private DateTime sTimes;
        private DateTime eTimes;
        Chart chart1 = new Chart();
        Series series1 = new Series();
        Series series2 = new Series();
        ChartArea chartArea1 = new ChartArea();
        List<IncidentAndChangeModel> icmSLA = new List<IncidentAndChangeModel>();
        List<SLAInfoModel> SLAInfoList = new List<SLAInfoModel>();
        string slaCategorychar1 = null;
        string slaCategorychar2 = null;
        int priority = 0;
        string viewName = null;

        //时间转换和为空处理
        private void timeSet(string sTime, string eTime)
        {
            sTimes = Convert.ToDateTime(sTime);
            eTimes = Convert.ToDateTime(eTime);
            if (sTime == null)
                sTimes = GlobalHelper.starTime;
            if (eTime == null)
                eTimes = GlobalHelper.endTime;
        }

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)sla).Dispose();
        }

        [InjectionConstructor]
        public SLAController(ISLA e)
        {
            sla = e;
        }

        //响应/解决率  
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime;slaCategorychar")]
        public ActionResult SLAToPriority(int? sid, int? bnid, string sTime, string eTime, string slaCategorychar)
        {
            //时间转换和为空处理
            timeSet(sTime, eTime);
            //处理slaCategorychar
            GetslaCategorycharAndPriorityToSLAToPriority(sid, bnid, slaCategorychar);
            if (icmSLA.FirstOrDefault(p => p.ID == 1) == null)
            {
                icmSLA.Add(new IncidentAndChangeModel { alls = 0, ID = 4 });
            }
            if (icmSLA.FirstOrDefault(p => p.ID == 4) == null)
            {
                icmSLA.Add(new IncidentAndChangeModel { alls = 0, ID = 4 });
            }
            if (icmSLA.FirstOrDefault(p => p.ID == 5) == null)
            {
                icmSLA.Add(new IncidentAndChangeModel { alls = 0, ID = 5 });
            }
            if (icmSLA.FirstOrDefault(p => p.ID == 7) == null)
            {
                icmSLA.Add(new IncidentAndChangeModel { alls = 0, ID = 5 });
            }
            double metNum=0;
            double missedNum =0;
            for (int i = 0; i < icmSLA.Count(); i++)
            {
                if (icmSLA[i].ID == 1)
                {
                     metNum += Convert.ToDouble(icmSLA[i].alls);
                }
                if (icmSLA[i].ID == 4)
                {
                    metNum += Convert.ToDouble(icmSLA[i].alls);
                }
                if (icmSLA[i].ID == 5)
                {
                    missedNum += Convert.ToDouble(icmSLA[i].alls);
                }
                if (icmSLA[i].ID == 7)
                {
                    missedNum += Convert.ToDouble(icmSLA[i].alls);
                }
            }
            double metNums = Convert.ToDouble((metNum / (metNum + missedNum)).ToString("0.00"));
            double missedNums = Convert.ToDouble((missedNum / (metNum + missedNum)).ToString("0.00"));
            double[] yval = { metNums, missedNums };
            string[] xval = { (metNums * 100).ToString() + "%", (missedNums * 100).ToString() + "%" };
            series1.Points.DataBindXY(xval, yval);
            series1.ChartType = SeriesChartType.Pie;
            series1.Points[0].LegendText = "满足";
            series1.Points[1].LegendText = "不满足";
            // 设置标签风格
            //series1["PieLabelStyle"] = "Outside";
            //图样式 concave/SoftEdge
            //series1["PieDrawingStyle"] = "SoftEdge";
            //ChartArea
            chartArea1.AxisY.LabelStyle.Format = "0%";
            //Chart
            chart1.Width = GlobalHelper.Width2;
            chart1.Height = GlobalHelper.Height2;
            //此属性设置为None才可以自定义调色板
            chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.TwoSetSLA;
            chart1.PaletteCustomColors = colorSet;
            Legend x = new Legend();
            x.BorderColor = Color.Black;
            chart1.Legends.Add(x);
            //设置图列样式颜色
            chart1.Legends[0].Font = GlobalHelper.fontLegend;
            //Title t = new Title();
            //if (priority == 1)
            //{
            //    t.Text = "高优先级";
            //}
            //if (priority == 2)
            //{
            //    t.Text = "中优先级";
            //}
            //if (priority == 3)
            //{
            //    t.Text = "低优先级";
            //}
            //t.Font = GlobalHelper.fontTitletoSLA;
            //chart1.Titles.Add(t);
            chart1.Series.Add(series1);
            chart1.ChartAreas.Add(chartArea1);
            return new ChartResult(chart1);
        }

        //供应商信息
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime;firmTo")]
        //firmTo = Incident/Change/Problem
        public ActionResult SLAToFirm(int? sid, int? bnid, string sTime, string eTime,string firmTo)
        {
            //时间转换和为空处理
            timeSet(sTime, eTime);
            List<SiteBranchAndWorkListModel> imsToFirm = new List<SiteBranchAndWorkListModel>();
            if (firmTo != null)
            {
                //I=事件
                if (firmTo == "Incident")
                {
                    //alls,sign,sign2,sign3 供应商，总数，解决数量，响应数量
                     imsToFirm = sla.SLAIncidentToFirm(sid, bnid, sTimes, eTimes);
                }
                //C=变更
                if (firmTo == "Change")
                {
                     //alls,sign,sign2,sign3 供应商，总数，解决数量，响应数量
                     imsToFirm = sla.SLAChangeToFirm(sid, bnid, sTimes, eTimes);
                }
                //P=问题
                if (firmTo == "Problem")
                {
                    //alls,sign,sign2,sign3 供应商，总数，解决数量，响应数量
                    imsToFirm = sla.SLAProblemToFirm(sid, bnid, sTimes, eTimes);
                }
            }
            
            //Series
            for (int i = 0; i < imsToFirm.Count(); i++)
            {
                double x1 = Convert.ToDouble(imsToFirm[i].sign2 / imsToFirm[i].sign);
                double y1 = Convert.ToDouble(imsToFirm[i].sign3 / imsToFirm[i].sign);
                series1.Points.AddXY(imsToFirm[i].alls, x1);
                series2.Points.AddXY(imsToFirm[i].alls, y1);
            }
            //图样式 concave/SoftEdge
            series1["PieDrawingStyle"] = "SoftEdge";
            //设置宽度
            series1["PointWidth"] = "0.5";
            series2["PointWidth"] = "0.5";
            //series1.BorderColor = System.Drawing.Color.FromArgb(189, 26, 59, 115);
            //series1.ChartArea = "Default";
            //series1.Legend = "Default";
            series1.Name = "响应率";
            series2.Name = "解决率";
            //series1.XValueType = ChartValueType.String;
            series1.LabelFormat = "0%";
            series2.LabelFormat = "0%";
            series1.ChartType = SeriesChartType.Column;
            series2.ChartType = SeriesChartType.Column;
            series1.IsValueShownAsLabel = true;
            series2.IsValueShownAsLabel = true;
            //ChartArea
            //设置XY轴刻度间隔
            chartArea1.AxisY.Interval = 0.2;
            chartArea1.AxisX.Interval = 1;
            //设置XY轴最大值
            chartArea1.AxisY.Maximum = 1;
            //chartArea1.AxisX.Maximum = 12;
            //设置墙的宽度为
            chartArea1.Area3DStyle.WallWidth = 1;
            //显示网格线
            chartArea1.AxisY.MajorGrid.Enabled = true;
            chartArea1.AxisX.Title = "(供应商)";
            //设置背景为白色
            chartArea1.BackColor = Color.White;
            chartArea1.Name = "Default";
            chartArea1.AxisY.LabelStyle.Format = "0%";
            //设置图标题（下方显示）
            //chartArea1.AxisX.Title = "供应商绩效考核";
            //Chart
            //设置柱状颜色
            //colChart.Palette = ChartColorPalette.Pastel;
            //此属性设置为None才可以自定义调色板
            chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.TwoSetPei;
            chart1.PaletteCustomColors = colorSet;

            //设置图列
            Legend x = new Legend("Legend1");
            x.BorderColor = Color.Black;
            //x.Title = "解决率";
            chart1.Legends.Add(x);
            //设置图列样式颜色
            chart1.Legends[0].Font = GlobalHelper.fontLegend;
            //设置图标题（上方显示）
            //Title t = new Title("供应商绩效考核");
            //t.Font = GlobalHelper.fontTitle;
            //chart1.Titles.Add(t);
            chart1.Series.Add(series1);
            chart1.Series.Add(series2);
            chart1.ChartAreas.Add(chartArea1);
            chart1.Size = new System.Drawing.Size(GlobalHelper.Width3, GlobalHelper.Height3);
            return new ChartResult(chart1);
        }

        //工单详细信息
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime;slaCategorychar;slaStatus")]
        public ActionResult SLAInfoToView(int? sid, int? bnid, string sTime, string eTime, string slaCategorychar, int slaStatus)
        {
            //时间转换和为空处理
            timeSet(sTime, eTime);
            //处理slaCategorychar
            GetslaCategorycharAndPriorityToView(sid, bnid, slaCategorychar, slaStatus);
            return PartialView(viewName, SLAInfoList);
        }

        //处理slaCategorychar SLAToPriority
        private void GetslaCategorycharAndPriorityToSLAToPriority(int? sid, int? bnid, string slaCategorychar)
        {
            if (slaCategorychar == "i1")
            {
                slaCategorychar1 = "事件响应时间";
                slaCategorychar2 = "Incident Response Time";
                priority = 1;
                icmSLA = sla.SLAHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "i2")
            {
                slaCategorychar1 = "事件响应时间";
                slaCategorychar2 = "Incident Response Time";
                priority = 2;
                icmSLA = sla.SLAHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "i3")
            {
                slaCategorychar1 = "事件响应时间";
                slaCategorychar2 = "Incident Response Time";
                priority = 3;
                icmSLA = sla.SLAHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "i4")
            {
                slaCategorychar1 = "事件解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 1;
                icmSLA = sla.SLAHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "i5")
            {
                slaCategorychar1 = "事件解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 2;
                icmSLA = sla.SLAHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "i6")
            {
                slaCategorychar1 = "事件解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 3;
                icmSLA = sla.SLAHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "c1")
            {
                slaCategorychar1 = "变更响应时间";
                slaCategorychar2 = "Change Resolution Time";
                priority = 1;
                icmSLA = sla.SLAChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "c2")
            {
                slaCategorychar1 = "变更响应时间";
                slaCategorychar2 = "Change Resolution Time";
                priority = 2;
                icmSLA = sla.SLAChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "c3")
            {
                slaCategorychar1 = "变更响应时间";
                slaCategorychar2 = "Change Resolution Time";
                priority = 3;
                icmSLA = sla.SLAChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "c4")
            {
                slaCategorychar1 = "变更解决时间";
                slaCategorychar2 = "Change Resolution Time";
                priority = 1;
                icmSLA = sla.SLAChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "c5")
            {
                slaCategorychar1 = "变更解决时间";
                slaCategorychar2 = "Change Resolution Time";
                priority = 2;
                icmSLA = sla.SLAChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "c6")
            {
                slaCategorychar1 = "变更解决时间";
                slaCategorychar2 = "Change Resolution Time";
                priority = 3;
                icmSLA = sla.SLAChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "p1")
            {
                slaCategorychar1 = "问题响应时间";
                slaCategorychar2 = "Problem Resolution Time";
                priority = 1;
                icmSLA = sla.SLAProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "p2")
            {
                slaCategorychar1 = "问题响应时间";
                slaCategorychar2 = "Problem Resolution Time";
                priority = 2;
                icmSLA = sla.SLAProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "p3")
            {
                slaCategorychar1 = "问题响应时间";
                slaCategorychar2 = "Problem Resolution Time";
                priority = 3;
                icmSLA = sla.SLAProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "p4")
            {
                slaCategorychar1 = "问题解决时间";
                slaCategorychar2 = "Problem Resolution Time";
                priority = 1;
                icmSLA = sla.SLAProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "p5")
            {
                slaCategorychar1 = "问题解决时间";
                slaCategorychar2 = "Problem Resolution Time";
                priority = 2;
                icmSLA = sla.SLAProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
            if (slaCategorychar == "p6")
            {
                slaCategorychar1 = "问题解决时间";
                slaCategorychar2 = "Problem Resolution Time";
                priority = 3;
                icmSLA = sla.SLAProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority);
            }
        }

        //处理slaCategorychar SLAInfoToView
        private void GetslaCategorycharAndPriorityToView(int? sid, int? bnid, string slaCategorychar, int slaStatus)
        {
            if (slaCategorychar == "i1")
            {
                slaCategorychar1 = "事件响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 1;
                SLAInfoList = sla.SLAInfoToHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToHelpDeskView";
            }
            if (slaCategorychar == "i2")
            {
                slaCategorychar1 = "事件响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 2;
                SLAInfoList = sla.SLAInfoToHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToHelpDeskView";
            }
            if (slaCategorychar == "i3")
            {
                slaCategorychar1 = "事件响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 3;
                SLAInfoList = sla.SLAInfoToHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToHelpDeskView";
            }
            if (slaCategorychar == "i4")
            {
                slaCategorychar1 = "事件解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 1;
                SLAInfoList = sla.SLAInfoToHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToHelpDeskView";
            }
            if (slaCategorychar == "i5")
            {
                slaCategorychar1 = "事件解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 2;
                SLAInfoList = sla.SLAInfoToHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToHelpDeskView";
            }
            if (slaCategorychar == "i6")
            {
                slaCategorychar1 = "事件解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 3;
                SLAInfoList = sla.SLAInfoToHelpDesk(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToHelpDeskView";
            }
            if (slaCategorychar == "c1")
            {
                slaCategorychar1 = "变更响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 1;
                SLAInfoList = sla.SLAInfoToChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToChangeView";
            }
            if (slaCategorychar == "c2")
            {
                slaCategorychar1 = "变更响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 2;
                SLAInfoList = sla.SLAInfoToChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToChangeView";
            }
            if (slaCategorychar == "c3")
            {
                slaCategorychar1 = "变更响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 3;
                SLAInfoList = sla.SLAInfoToChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToChangeView";
            }
            if (slaCategorychar == "c4")
            {
                slaCategorychar1 = "变更解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 1;
                SLAInfoList = sla.SLAInfoToChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToChangeView";
            }
            if (slaCategorychar == "c5")
            {
                slaCategorychar1 = "变更解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 2;
                SLAInfoList = sla.SLAInfoToChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToChangeView";
            }
            if (slaCategorychar == "c6")
            {
                slaCategorychar1 = "变更解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 3;
                SLAInfoList = sla.SLAInfoToChange(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToChangeView";
            }
            if (slaCategorychar == "p1")
            {
                slaCategorychar1 = "问题响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 1;
                SLAInfoList = sla.SLAInfoToProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToProblemView";
            }
            if (slaCategorychar == "p2")
            {
                slaCategorychar1 = "问题响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 2;
                SLAInfoList = sla.SLAInfoToProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToProblemView";
            }
            if (slaCategorychar == "p3")
            {
                slaCategorychar1 = "问题响应时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 3;
                SLAInfoList = sla.SLAInfoToProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToProblemView";
            }
            if (slaCategorychar == "p4")
            {
                slaCategorychar1 = "问题解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 1;
                SLAInfoList = sla.SLAInfoToProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToProblemView";
            }
            if (slaCategorychar == "p5")
            {
                slaCategorychar1 = "问题解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 2;
                SLAInfoList = sla.SLAInfoToProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToProblemView";
            }
            if (slaCategorychar == "p6")
            {
                slaCategorychar1 = "问题解决时间";
                slaCategorychar2 = "Incident Resolution Time";
                priority = 3;
                SLAInfoList = sla.SLAInfoToProblem(sid, bnid, sTimes, eTimes, slaCategorychar1, slaCategorychar2, priority, slaStatus);
                viewName = "SLAInfoToProblemView";
            }
        }
     
    }
}
